﻿@using si.caha.Repository;
@using si.caha.Model;

@{
    Layout = "/tpl/_layout.cshtml";
    PageData["PageTitle"] = "Order details";

    var orderId = UrlData[0].IsInt() ? UrlData[0].AsInt() : 0;
    var address = new Address();
    var user = new User();
    var orderValue = 0.0;

    var dataContext = new DataContext();
    var orderRepository = new OrderRepository(dataContext);
    var order = orderRepository.Get(orderId);

    if (order != null)
    {
        var userRepository = new UserRepository(dataContext);
        var addressRepository = new AddressRepository(dataContext);
        var itemRepository = new ItemRepository(dataContext);
        
        user = userRepository.Get(order.UserId);
        address = addressRepository.Get(order.AddressId);
    }
    else
    {
        var orderedItem = new OrderedItem();
        orderedItem.Item = new Item();
        order = new Order();
        order.OrderedItems = new List<OrderedItem>() { orderedItem };
    }
}
@if (order.Completed)
{
    <div>Order is completed</div>
}
else
{
    <div>Order is not completed</div>
}
<div>User: @user.Email</div>
@if (address != null)
{
    <li class="address">
        <table>
            <tr>
                <td><span>Ime</span></td>
                <td><span>@address.Firstname</span></td>
            </tr>
            <tr>
                <td><span>Priimek</span></td>
                <td><span>@address.Surname</span></td>
            </tr>
            <tr>
                <td><span>Ulica in hišna številka</span></td>
                <td><span>@address.StreetWithHouseNumber</span></td>
            </tr>
            <tr>
                <td><span>Poštna številka</span></td>
                <td><span>@address.PostNumber</span></td>
            </tr>
            <tr>
                <td><span>Pošta</span></td>
                <td><span>@address.Post</span></td>
            </tr>
            <tr>
                <td><span>Država</span></td>
                <td><span>@address.Country</span></td>
            </tr>
        </table>
    </li>
}
<ul>
    @foreach (var orderedItem in order.OrderedItems)
    {
        <li><a href="@Href("~/Store/Item/" + @orderedItem.Item.ItemId)">@orderedItem.Item.Title</a> - (@orderedItem.Item.Price€ x @orderedItem.Quantity)</li>
        orderValue += (orderedItem.Item.Price * orderedItem.Quantity);
    }
</ul>
<div>
    Total Price: @orderValue€
<div>
<div>
<a href=@Href("~/Admin/Orders")>Back to orders</a>
</div>